//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <string.h>
#include <elatypes.h>

EXTERN_C void * CDECL memchr(const void * buf, int c, register size_t count)
{
#ifdef _GNUC

#if defined(_x86)
    int temp;
    register void * pDest;
    if (!count)
        return NULL;

    ASM("repne;"
        "scasb;"
        "je 1f;"//if match, jump to 1:
        "movl $1,%0;"//if not match, make di=1 and -1 below, so di=0(pDest=di)
        "1:"
        "decl %0;"//di would +1 after scasb, so need to -1
        :"=D" (pDest), "=c" (temp)
        :"0" (buf), "a" (c), "1" (count));
    return pDest;
#endif    //if defined(_x86)

#endif    //ifdef _GNUC

    const char * p = (const char *)buf;

    for (; count != 0; count--) {
        if (*p++== (char)c)
            return ((void *)(p-1));
    }

    return 0;
}
